<?php
/**
 * Description of Posts
 *
 * @author Suraj
 */

class Model_Posts extends Zend_Db_Table {
	protected $_name="posts";

    //get multiple posts as the query.
    function getPostswithCommnets () {
        //$db = Zend_Db_Table::getDefaultAdapter();
        $db = $this->getDefaultAdapter();
        $newselect = new Zend_Db_Select($db);

        $newselect->from ( array ('p'=>'posts'))
                  ->joinLeft ( array ('c'=>'comments'),'p.id=c.post_id',array ('num_comm'=>'COUNT(post_id)'))
                  ->group('p.id');

        //$newselect="SELECT  `posts`.* FROM `posts`";
        $exe_query= $db->query($newselect);
        //$x= $newselect->query();
        $result = $exe_query->fetchAll();
        return $result;
    }

   /* function getPosts() {
        $orderby = array('id DESC');
        $result = $this->fetchAll('1',$orderby);
        return $result->toArray();
    }*/

    // only get single post as mentioned for the id
    function getPost($id) {
        $val=$this->fetchRow("id =".$id);
        return $val->toArray();
    }

    //saves the post just need to pass the value in array
    function savePost($data) {
        $query= array (
            'Title'=>$data['Title'],
            'Description'=>$data['Description'],
        );
        return $this->insert($query);
    }

    //updates the post, the argument passed should be in arra format.
    function updatePost($data) {
        $query= array (            
            'Title'=>$data['Title'],
            'Description'=>$data['Description'],
        );
        return $this->update($query, 'id = '.$data['id']);
    }

    // delete the post and the argument passsed should be an id.
    function deletePost($id) {
        return $this->delete('id = '.$id);
    }
}
?>
